package com.hzhr.iot.collect.protocol.processor;

import com.hzhr.iot.collect.constant.MessageFieldKeyEnum;
import com.hzhr.iot.collect.protocol.util.CachedCommandUtil;
import com.hzhr.iot.collect.protocol.util.MessageUtil;
import com.hzhr.iot.collect.service.helper.Device_IotDeviceServiceHelper;
import com.hzhr.iot.collect.service.helper.Device_IotDevice_RegistInfoServiceHelper;
import com.hzhr.iot.collect.util.StrUtil;
import com.hzhr.iot.core.util.HzhrStringUtils;
import com.hzhr.iot.device.sdk.model.iotDevice.IotDeviceModel;
import com.hzhr.iot.device.sdk.model.iotDevice_registInfo.IotDevice_RegistInfoPutModel;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 压力监测仪 注册
 * @author 48310
 *
 */
@Component
public class MessageProcessor42 implements MessageProcessor {
	@Autowired
	MessageUtil messageUtil;
	@Autowired
	Device_IotDeviceServiceHelper device_iotDeviceServiceHelper;
	@Autowired
	CachedCommandUtil cachedCommandUtil;
	@Autowired
	Device_IotDevice_RegistInfoServiceHelper device_iotDevice_registInfoServiceHelper;

	@Override
	public List<Map<MessageFieldKeyEnum, Object>> proccess(int protocolId, String deviceId, Map<MessageFieldKeyEnum, Object> parsedMessageObj) throws Exception {
		List<Map<MessageFieldKeyEnum, Object>> responseMsgList = new ArrayList<>();//响应报文列表

		Map<MessageFieldKeyEnum, Object> content = new HashMap<>();

		//生成上下行密钥
		String upKey = RandomUtils.nextInt(255) + "";
		String downKey = RandomUtils.nextInt(255) + "";

		//生成初始上报时间
		String hour = "00";
		String min = HzhrStringUtils.FillZeroForLeft(RandomUtils.nextInt(30) + "", 2);
		String sec = HzhrStringUtils.FillZeroForLeft(RandomUtils.nextInt(60) + "", 2);
		String reportTime = hour + min + sec;

		//根据物联网设备代码查询设备
		String iotDeviceCode = parsedMessageObj.get(MessageFieldKeyEnum.IOT_DEVICE_ID).toString();
		IotDeviceModel oriIotDevice = device_iotDeviceServiceHelper.getByCode(iotDeviceCode);
		String oriDeviceId = null;
		if (oriIotDevice != null) {
			oriDeviceId = oriIotDevice.getIotPlatformDeviceId();
			if (!deviceId.equals(oriDeviceId)) {
				//组装响应
				content.put(MessageFieldKeyEnum.UP_KEY, upKey);
				content.put(MessageFieldKeyEnum.DOWN_KEY, downKey);
				content.put(MessageFieldKeyEnum.RESP_WAKE_TIME, reportTime);
				String respCode = "0111";
				Map<MessageFieldKeyEnum, Object> respMsg = messageUtil.generatePressureMonitorResponse(protocolId, parsedMessageObj, respCode, content);
				responseMsgList.add(respMsg);
			}
		}

		//查询iot平台设备编号设备
		IotDeviceModel iotDeviceModel = device_iotDeviceServiceHelper.getByIotPlatformDeviceId(deviceId);
		if (iotDeviceModel == null) return null;

		//写入注册信息
		IotDevice_RegistInfoPutModel iotDevice_registInfoPutModel = new IotDevice_RegistInfoPutModel();
		iotDevice_registInfoPutModel.setCode(iotDeviceCode);
		iotDevice_registInfoPutModel.setDownKey(StrUtil.str2hexStr(downKey));
		iotDevice_registInfoPutModel.setUpKey(StrUtil.str2hexStr(upKey));
		iotDevice_registInfoPutModel.setReportTime(reportTime);
		device_iotDevice_registInfoServiceHelper.putByIotDeviceId(iotDeviceModel.getId(), iotDevice_registInfoPutModel);

		//组装响应
		content.put(MessageFieldKeyEnum.UP_KEY, upKey);
		content.put(MessageFieldKeyEnum.DOWN_KEY, downKey);
		content.put(MessageFieldKeyEnum.RESP_WAKE_TIME, reportTime);
		String respCode = null;
		if (deviceId.equals(oriDeviceId)) {
			respCode = "0102";
		} else {
			respCode = "0000";
		}
		Map<MessageFieldKeyEnum, Object> respMsg = messageUtil.generatePressureMonitorResponse(protocolId, parsedMessageObj, respCode, content);
		responseMsgList.add(respMsg);

		//清除该设备的所有缓存
		cachedCommandUtil.deleteCachedCommand(deviceId);

		return responseMsgList;
	}
}
